package com.ruoyi.lab.utils;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.lab.enums.RoleEnum;
import com.ruoyi.system.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class RoleUtils {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 判断是否具有学生角色
     * @return
     */
    public Boolean isStudent() {
        List<SysRole> roleList = sysRoleService.selectRolesByUserName(SecurityUtils.getUsername());
        for (SysRole sysRole : roleList) {
            if (RoleEnum.ROLE_STUDENT.getRoleKey().equals(sysRole.getRoleKey())) {
                log.info("此用户具有学生身份");
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否具有教师角色
     * @return
     */
    public Boolean isTeacher() {
        List<SysRole> roleList = sysRoleService.selectRolesByUserName(SecurityUtils.getUsername());
        for (SysRole sysRole : roleList) {
            if (RoleEnum.ROLE_TEACHER.getRoleKey().equals(sysRole.getRoleKey())) {
                log.info("此用户具有教师身份");
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否具有院长角色
     * @return
     */
    public Boolean isAcademy() {
        List<SysRole> roleList = sysRoleService.selectRolesByUserName(SecurityUtils.getUsername());
        for (SysRole sysRole : roleList) {
            if (RoleEnum.ROLE_ACADEMY.getRoleKey().equals(sysRole.getRoleKey())) {
                log.info("此用户具有教学院长身份");
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否具有校管理员角色
     * @return
     */
    public Boolean isSchoolAdmin() {
        List<SysRole> roleList = sysRoleService.selectRolesByUserName(SecurityUtils.getUsername());
        for (SysRole sysRole : roleList) {
            if (RoleEnum.ROLE_SCHOOL.getRoleKey().equals(sysRole.getRoleKey())) {
                log.info("此用户具有校管理员身份");
                return true;
            }
        }
        return false;
    }
}
